package com.turnengine.client.local.action.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.action.bean.IAction;
import com.turnengine.client.local.action.bean.IActionCondition;
import com.turnengine.client.local.action.bean.IActionTarget;
import com.turnengine.client.local.action.command.AddAction;
import com.turnengine.client.local.action.command.AddActionCondition;
import com.turnengine.client.local.action.command.AddActionTarget;
import com.turnengine.client.local.action.command.GetActionConditionList;
import com.turnengine.client.local.action.command.GetActionList;
import com.turnengine.client.local.action.command.GetActionTargetList;
import com.turnengine.client.local.action.command.IAddAction;
import com.turnengine.client.local.action.command.IAddActionCondition;
import com.turnengine.client.local.action.command.IAddActionTarget;
import com.turnengine.client.local.action.command.IGetActionConditionList;
import com.turnengine.client.local.action.command.IGetActionList;
import com.turnengine.client.local.action.command.IGetActionTargetList;
import com.turnengine.client.local.action.command.IStartActionAtPlayer;
import com.turnengine.client.local.action.command.StartActionAtPlayer;
import com.turnengine.client.local.action.enums.ActionConditionExecute;
import com.turnengine.client.local.action.enums.ActionConditionMandatory;
import com.turnengine.client.local.action.enums.ActionConditionType;
import com.turnengine.client.local.action.enums.ActionTargetType;
import com.turnengine.client.local.unit.enums.UnitRelation;
import com.turnengine.client.local.unit.enums.UnitType;
import java.util.List;

/**
 * The Local Action Command Set.
 */
public class LocalActionCommandSet implements ILocalActionCommandSet {

	@Override
	public IAction addAction(long loginId, int instanceId, int actionId, int turns, int limit) {
		IAddAction command = new AddAction(loginId, instanceId, actionId, turns, limit);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IAction> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IActionTarget addActionTarget(long loginId, int instanceId, int actionId, ActionTargetType target, UnitType type, UnitRelation relation) {
		IAddActionTarget command = new AddActionTarget(loginId, instanceId, actionId, target, type, relation);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IActionTarget> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IActionCondition addActionCondition(long loginId, int instanceId, int actionId, ActionTargetType targetType, ActionConditionType type, ActionConditionExecute execute, ActionConditionMandatory mandatory, int conditionId, long conditionAmount) {
		IAddActionCondition command = new AddActionCondition(loginId, instanceId, actionId, targetType, type, execute, mandatory, conditionId, conditionAmount);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IActionCondition> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IAction> getActionList(long loginId, int instanceId) {
		IGetActionList command = new GetActionList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IAction>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IActionTarget> getActionTargetList(long loginId, int instanceId) {
		IGetActionTargetList command = new GetActionTargetList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IActionTarget>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IActionCondition> getActionConditionList(long loginId, int instanceId) {
		IGetActionConditionList command = new GetActionConditionList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IActionCondition>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean startActionAtPlayer(long loginId, int instanceId, int id, long amount) {
		IStartActionAtPlayer command = new StartActionAtPlayer(loginId, instanceId, id, amount);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
